/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddLayerForm.java
 *
 * Created on May 11, 2011, 1:53:14 PM
 */
package com.iceberg.adgt.ui.gen;

import com.iceberg.adgt.common.DomainException;
import com.iceberg.adgt.common.Observer;
import com.iceberg.adgt.common.ObserverInterest;
import com.iceberg.adgt.domain.DomainFacade;
import com.iceberg.adgt.domain.DomainFacadeImpl;
import com.iceberg.adgt.ui.architecture.ArchitectureController;

import java.util.List;

/**
 * @author David
 */
public class AddLayerForm extends javax.swing.JPanel implements Observer {

	/**
	 * Creates new form AddLayerForm
	 */
	public AddLayerForm() {
		initComponents();
		init();
	}

	/**
	 * This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		addLayerLabel = new javax.swing.JLabel();
		addLayerField = new javax.swing.JTextField();
		addLayerButton = new javax.swing.JButton();
		addLayerErrorMsg = new javax.swing.JLabel();
		layerNumberBox = new javax.swing.JComboBox();

		addLayerLabel.setText("Name:");

		addLayerField.setText("Layer Name");

		addLayerButton.setText("Add layer");
		addLayerButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				addLayerButtonActionPerformed(evt);
			}
		});

		addLayerErrorMsg.setForeground(java.awt.Color.red);

		layerNumberBox.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"Item 1", "Item 2", "Item 3", "Item 4"}));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(layout.createSequentialGroup()
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
										.addGroup(layout.createSequentialGroup()
												.addComponent(addLayerLabel)
												.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
												.addComponent(addLayerField, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
												.addGap(6, 6, 6)
												.addComponent(layerNumberBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
												.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
												.addComponent(addLayerButton))
										.addComponent(addLayerErrorMsg))
								.addContainerGap(20, Short.MAX_VALUE))
		);
		layout.setVerticalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(layout.createSequentialGroup()
								.addComponent(addLayerErrorMsg, javax.swing.GroupLayout.DEFAULT_SIZE, 18, Short.MAX_VALUE)
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
										.addComponent(addLayerLabel)
										.addComponent(addLayerField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
										.addComponent(addLayerButton)
										.addComponent(layerNumberBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
		);
	}// </editor-fold>//GEN-END:initComponents

	private void addLayerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addLayerButtonActionPerformed

		try {
			this.controller.addLayer(this.addLayerField.getText(), (Integer) this.layerNumberBox.getSelectedItem());
			addLayerErrorMsg.setText("");
		} catch (DomainException de) {
			addLayerErrorMsg.setText(de.getMessage());
		}

	}//GEN-LAST:event_addLayerButtonActionPerformed

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton addLayerButton;
	private javax.swing.JLabel addLayerErrorMsg;
	private javax.swing.JTextField addLayerField;
	private javax.swing.JLabel addLayerLabel;
	private javax.swing.JComboBox layerNumberBox;
	// End of variables declaration//GEN-END:variables
	private ArchitectureController controller;

	private void setNumbers() {
		List<Integer> usedLayerNumbers = this.controller.getUsedLayerNumbers();

		this.layerNumberBox.removeAllItems();

		for (int s = 1; s < 101; s++) {
			if (!usedLayerNumbers.contains(s)) {
				this.layerNumberBox.addItem(s);
			}
		}
	}

	public final void setController(ArchitectureController controller) {
		this.controller = controller;
		this.setNumbers();
	}

	@Override
	public final void notifyChange(ObserverInterest interest) {
		this.setNumbers();
	}

	private void init() {
		DomainFacade facade = DomainFacadeImpl.getInstance();
		facade.addObserver(this, ObserverInterest.LAYERS_CHANGED);
	}
}